require "Common/define"


ParticleSystemEffectParam = {};
ParticleSystemEffectParam.__index = ParticleSystemEffectParam;

local cacheList = {}

function ParticleSystemEffectParam.Get(effectRes, depth, flip)
    local obj = table.remove(cacheList, 1);
    if obj then
        obj.effectRes = effectRes;

        obj.depth = depth or 0; --0;

        obj.flip = flip or false;

        obj.follow = false;
        obj.followInfo = nil;

        obj:setColor(0, 0, 0, 0);

        obj.delayEndTime = 0;

        obj.fighterId = 0;

        if obj.pos then
            obj.pos.x, obj.pos.y, obj.pos.z = 0, 0, 0;
        else
            obj.pos = Vector3.zero;
        end

        if obj.offset then
            obj.offset[1], obj.offset[2] = 0, 0;
        else
            obj.offset = {0, 0};
        end

        return obj;
    end
    return ParticleSystemEffectParam:new(effectRes, depth, flip)
end

function ParticleSystemEffectParam.Put(obj)
    if not obj then
        return
    end

    obj.effectRes = nil;
    obj.depth = nil; --0;

    --obj.pos = Vector3.zero;
    --obj.offset = {0, 0};

    obj.flip = nil;
    obj.follow = false;
    obj.followInfo = nil;
    --obj.color = Color(0, 0, 0, 0);
    obj.delayEndTime = 0;
    obj.fighterId = 0;

    table.insert(cacheList, obj)
end

function ParticleSystemEffectParam:new(effectRes, depth, flip)
    local object ={};

    object.effectRes = effectRes;

    object.depth = depth or 0; --0;

    object.pos = Vector3.zero;
    object.offset = {0, 0};

    --object.duration = nil;-- -1
    --object.loop = false;
    object.flip = flip or false;

    object.follow = false;
    object.followInfo = nil;

    object.color = Color(0, 0, 0, 0);

    object.delayEndTime = 0;

    object.fighterId = 0;

    setmetatable(object, ParticleSystemEffectParam);
    return object;
end


function ParticleSystemEffectParam:setPosition(position)
    self.pos = position;
end

--followTrans 为跟随transform的方式， followBone, followBoneSpineTrans为跟随spine bone的方式， 两组值设置其中之一
function ParticleSystemEffectParam:setFollow(followTrans, offset, followBone, followBoneSpineTrans)
    self.follow = true;

    self.followInfo = {};
    self.followInfo.followTrans = followTrans;

    self.followInfo.followBone = followBone;
    self.followInfo.followBoneSpineTrans = followBoneSpineTrans;

    self.followInfo.offset = offset or {0, 0};

    --logMy('ParticleSystemEffectParam:setFollow', self.followInfo, followTrans, offset, followBone, followBoneSpineTrans);
end

function ParticleSystemEffectParam:setColor(r, g, b, a)
    local color = self.color;
    color.r, color.g, color.b, color.a = r, g, b, a;
end

function ParticleSystemEffectParam:setFighterId(fighterId)
    self.fighterId = fighterId;
end

function ParticleSystemEffectParam:setDelayEndTime(delayEndTime)
    self.delayEndTime = delayEndTime or 0;
end

